package com.sunplus.common.core.ossstorage.aliyunstorage;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.sunplus.common.core.ossstorage.inteface.IStorageFile;
import com.sunplus.common.core.ossstorage.inteface.IStorageFolder;
import com.sunplus.common.core.ossstorage.inteface.IStorageProvider;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

/**
 * @author shaoqi
 * @ClassName AliyunOssProvicer
 * @Description TODO
 * @date 2021/2/9 0009
 */
public class AliyunOssProvicer implements IStorageProvider {


    private OSS client;

    /**
     * bucket的名字
     */
    private String bucketName;

    public AliyunOssProvicer(String endpoint, String accesskeyId, String accessKeySecret, String bucketName) {
        client = new OSSClientBuilder().build(endpoint, accesskeyId, accessKeySecret);
        this.bucketName = bucketName;
    }


    @Override
    public Boolean fileExists(String path) {
        path = AliyunOssUtil.fixFilePath(path);
        try {
            OSSObject fileObject = client.getObject(bucketName, path);
            return fileObject != null && !(fileObject.getKey() == null || "".equals(fileObject.getKey()));
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public IStorageFile getFile(String path) {
        path = AliyunOssUtil.fixFilePath(path);
        OSSObject fileObject = client.getObject(bucketName, path);
        // String rootpath="";
        return new AliyunOssFile(client,fileObject,"");
    }

    @Override
    public Collection<IStorageFolder> listFiles(String path) {
        return null;
    }

    @Override
    public void deleteFile(String path) {

    }

    @Override
    public void copyFile(String originalPath, String duplicatePath) {

    }

    @Override
    public IStorageFolder createFile(String path) {
        path = AliyunOssUtil.fixFilePath(path);
        client.putObject(bucketName, path, new InputStream() {
            @Override
            public int read() throws IOException {
                return 0;
            }
        });


        return null;
    }

    @Override
    public boolean trySaveStream(String path, InputStream inputStream) {
        return false;
    }

    @Override
    public void saveStream(String path, InputStream inputStream) {
        path = AliyunOssUtil.fixFilePath(path);
        //inputStream.reset();
    }

    @Override
    public Boolean folderExists(String path) {
        return null;
    }

    @Override
    public Collection<IStorageFolder> listFolders(String path) {
        return null;
    }

    @Override
    public Boolean tryCreateFolder(String path) {
        return null;
    }

    @Override
    public void createFolder(String path) {

    }

    @Override
    public void deleteFolder(String path) {

    }

    @Override
    public void renameFolder(String oldPath, String newPath) {

    }
}
